package cn.hdu.liang.ch2;

public class CheckingAccount {
    private volatile int balance;

    public CheckingAccount(int balance) {
        this.balance = balance;
    }

    public synchronized boolean withdraw(int amount) {
        if(amount <= balance) {
            try {
                Thread.sleep((int) (Math.random() * 200));
            } catch(InterruptedException ie) {

            }

            balance -= amount;
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        final CheckingAccount ca = new CheckingAccount(100);
        Runnable r = () -> {
            String name = Thread.currentThread().getName();
            for(int i = 0; i < 10; i++) {
                System.out.println(name + " withdraws $10: " + ca.withdraw(10));
            }
        };

        Thread thdHusband = new Thread(r);
        thdHusband.setName("Husband");

        Thread thdWife = new Thread(r);
        thdWife.setName("Wife");

        thdHusband.start();
        thdWife.start();
    }
}
